﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using System.Collections.Generic;
using SilverlightApp.data;
using System.Collections;
using System.Collections.ObjectModel;
using SilverlightApp.Common;

namespace SilverlightApp.Web
{

    public class HumidityChartBuilder
    {
        private MyObservableCollection<PairInfo> DataSource = null;
        private Chart chart = null;

        public void BuildChart(Panel panel, MyObservableCollection<PairInfo> ds)
        {
            DataSource = ds;
            chart = new Chart { Title = "湿度监控" };
            chart.Height = 230;
            Style legendStyle = new System.Windows.Style();
            Setter legendWidth = new Setter(System.Windows.Controls.Control.WidthProperty, 0);
            Setter legendHeight = new Setter(System.Windows.Controls.Control.HeightProperty, 0);
            legendStyle.Setters.Add(legendWidth);
            legendStyle.Setters.Add(legendHeight);
            legendStyle.TargetType = typeof(System.Windows.Controls.Control);
            chart.LegendStyle = legendStyle;

            ColumnSeries series = new ColumnSeries();
            series.ItemsSource = ds;
            var lis = series.LegendItems;
            series.Title = 0 + "°C";
            ds.AfterPropertyChanged = (self) =>
            {
                if (self != null && self.Count > 0)
                {
                    chart.Title = string.Format("湿度监控" + Environment.NewLine + "({0}°C)", Math.Round(self[0].Value, 2));
                }
            };
            series.Title = null;
            series.IndependentValueBinding = new System.Windows.Data.Binding("Name");
            series.DependentValueBinding = new System.Windows.Data.Binding("Value");
            Style lineStyle = new System.Windows.Style();
            lineStyle.TargetType = typeof(System.Windows.Controls.Control);
            Setter setterRed = new Setter(System.Windows.Controls.Control.BackgroundProperty, new SolidColorBrush(Colors.Blue));
            Setter width = new Setter(System.Windows.Controls.Control.WidthProperty, 20);
            lineStyle.Setters.Add(setterRed);
            lineStyle.Setters.Add(width);
            series.DataPointStyle = lineStyle;
            LinearAxis valueAxis = new LinearAxis { Orientation = AxisOrientation.Y, Minimum = 0, Maximum = 100, ShowGridLines = true };
            valueAxis.Interval = 20;
            chart.Axes.Add(valueAxis);
            chart.Series.Add(series);
            //chart.Series.Add(series);
            Grid grid = new Grid();
            var hei = chart.Width;
            grid.Children.Add(chart);
            panel.Children.Add(grid);
        }
    }
}
